var m = require("../utils/md5.js");
function sort(data)
{
    var keys = Object.keys(data).sort();
    var result = {};
    for(var i = 0; i < keys.length; i++){
        var key = keys[i];
        result[key] = data[key];
    }
    return result;
}

function jsonVAL(json)
{
    var v = '';
    for(var i in json){
        v += json[i];
    }
    return v;
}

function getTimestamp()
{
    return Date.parse(new Date()) / 1000;
}

function mtRand(min, max)
{
    var result = Math.random() * (max - min + 1) + min;
    return parseInt(result);
}

function sign(appkey, timespan)
{
    var json={};
    // json.timestamp = getTimestamp();
    json.timestamp = timespan;
    //json.rand = mtRand(100000, 999999);
    json.appkey = appkey;
    //json.sign = m.md5(jsonVAL(sort(json)));
    json.sign = m.hexMD5(jsonVAL(json));
    //delete json.timestamp;
    return json.sign;
}

var x = {
    sign: sign,
    getTimestamp:getTimestamp
};
module.exports = x;